<?php

namespace App\Http\Controllers\Guard\User;

use App\Http\Controllers\Common\GuardController;
use App\Models\Rbac\GuardUser;
use App\Models\User\FlyOrder;
use App\Models\User\PointLog;
use App\Models\User\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class UserController extends GuardController {

    public function __construct() {
        $this->instance = new User();
    }


    public function lst(Request $request) {

        if($request->has('all')){
            $records = $this->instance->newQuery()->get(['id','name','passport','phone']);
            return responseSuccess($records->toArray());
        }

        $conditions = $request->input();
        $records = $this->instance->getLst($conditions)->paginate($conditions['pageSize'], $this->getCol, 'page', $conditions['page']);
        return responseSuccess($records->items(), $records->total());
    }

    public function pointlog(Request $request) {
        $conditions = $request->input();
        $records = (new PointLog())->getLst($conditions)
            ->with('getUser')
            ->paginate($conditions['pageSize'], $this->getCol, 'page', $conditions['page']);
        foreach ($records as $record) {
            $record->point = $record->action=='add'?$record->point:'-'.$record->point;
        }
        return responseSuccess($records->items(), $records->total());
    }

    public function add(Request $request) {
        $this->validate($request, [
            'name' => 'required',
            'passport' => 'required',
            'phone' => 'required',
            'point' => 'required|integer|gt:0',

        ]);

        $id = $request->input('id', 0);
        $name = $request->input('name');
        $passport = $request->input('passport');
        $phone = $request->input('phone');
        $birthday = $request->input('birthday');
        $beizhu = $request->input('beizhu');
        $point = $request->input('point');
        $status = $request->input('status', 1);
        $pwd = Str::substr($phone, -4);

        $diffPoint = 0;
        if ($id > 0) {
            //计算分差
            $user = $this->instance->newQuery()->find($id);
            $diffPoint = $point - $user->point;
        }
        try {
            DB::beginTransaction();
            $conditions = compact('name', 'passport', 'phone', 'birthday', 'beizhu', 'status', 'pwd');
            $user = $this->instance->updateOrCreate(compact('id'), $conditions);

            //积分log
            $token = $request->header('token');
            $guardUserId = checkJWT($token)['id'];
            $guardUser = GuardUser::query()->find($guardUserId);
            if ($id <= 0) {
                $user->addPoint($point, "创建新用户 $name");
            } else {
                //因为上边已经修改过point了，所有addPoint是0
                if ($diffPoint > 0) {
                    $user->addPoint($diffPoint, "$guardUser->name 给 $name 加了 $diffPoint,积分最终变成了 $point");
                } elseif ($diffPoint<0) {
                    $diffPoint = abs($diffPoint);
                    $user->diviPoint($diffPoint, "$guardUser->name 给 $name 减了 $diffPoint ,积分最终变成了 $point");
                }else{

                }
            }
            DB::commit();
            return responseSuccess($user->toArray());
        } catch (\Exception $e) {
            DB::rollBack();
            $tip = $e->getFile() . ':' . $e->getLine() . ':' . $e->getMessage();
            return responseFail($tip);
        }


    }


}
